DLSS.Main = Ext.extend(Ext.Panel, {
    constructor:function(){
        
        var eventsGrid = new Ext.grid.EditorGridPanel({
            cellCls:'event-management-table',
            store: DLSS.eventsStore,
            cm: DLSS.eventsColumnModel,
            sm: DLSS.Main.sm,
            width:1000,
            height:275,
            autoExpandColumn:'event',
            title:'Active Events',
            frame:true,
            viewConfig: {
                forceFit: true
            },
            clicksToEdit:2,
            tbar: [{
                text: DLSS.getMessage('add'),
                handler : function(){
                    var u = new DLSS.Event({
                        title:'',
                        date:'',
                        type:'',
                        rank:'',
                        place:'',
                        organizer:'',
                        masterOfCeremony:'',
                        chairman:'',
                        music:'',
                        scrutineer:'',
                        support:''
                    });
                    eventsGrid.stopEditing();
                    DLSS.eventsStore.insert(0, u);
                    eventsGrid.startEditing(0, 0);
                    eventsGrid.fireEvent('afteredit',{
                        record:u,
                        grid:eventsGrid,
                        row:0
                    });
                }
            },{
                text: DLSS.getMessage('delete'),
                handler:function() {
                    if (!eventsGrid.getSelectionModel().hasSelection()) {
                        Ext.Msg.show({
                            title:'Information',
                            msg: 'First select an event.',
                            buttons: Ext.Msg.OK,
                            animEl: eventsGrid.getTopToolbar().items.get(1).getEl(),
                            icon: Ext.Msg.INFO
                        });
                        return;
                    }
                    var r = eventsGrid.getSelectionModel().getSelected();
                    Ext.Ajax.request({url:'getCurrentEvent.htm',method:'GET',
            			success:function(rsp) {
                			var o = Ext.util.JSON.decode(rsp.responseText);
                			if (o.event) {
						        if (r.get('id')==o.event) {
			                        Ext.Msg.show({title:'Warning!', msg: 'It is unable to delete current event',
			                            buttons: Ext.Msg.OK, 
			                            animEl: eventsGrid.getTopToolbar().items.get(1).getEl(),
			                            icon: Ext.Msg.WARNING
			                        });
			                        return;
			                    }
                    		}
                    	}
                    });
                    Ext.Msg.show({
                        title:'Warning!',
                        msg: 'Are you realy want to delete event '+r.get('title')+'?',
                        buttons: Ext.Msg.YESNO,
                        fn: function(buttonId){
                            if (buttonId!='yes') return;
                            Ext.Ajax.request({
                                url:'eventDelete.htm',
                                params:{
                                    id:this.r.get('id')
                                },
                                success:function() {
                                    this.s.remove(this.r);
                                },
                                scope:this,
                                method:'POST'
                            })
                        },
                        scope:{r:r,s:eventsGrid.getStore()},
                        animEl: eventsGrid.getTopToolbar().items.get(1).getEl(),
                        icon: Ext.Msg.QUESTION
                    });
                }
            }]
        });

        eventsGrid.on('afteredit', this.submitEdition,this);
        
        /*eventsGrid.getSelectionModel().on('cellselect', function(sm, rowIndex){
            if(CURRENT_EVENT==DLSS.eventsStore.getAt(rowIndex).get('id')) return;
            //CURRENT_EVENT = DLSS.eventsStore.getAt(rowIndex).get('id');
        },this);*/

        DLSS.Main.superclass.constructor.call(this,{
            frame:false,
            header:false,
            border:false,
            layout:'table',
            bodyStyle:'padding:10px;',
            layoutConfig:{
                columns:2
            },
            items:[
                eventsGrid,
            ],
            tbar:DLSS.COMPONENTS_ROOT.getToolBarForPage(1)
        });

        this.on('show',function() {
            DLSS.eventsStore.load();//this.eventsStore.load();
        },this);

    },
    submitEdition:function(editEvent) {
        var r = editEvent.record;
        if (r.get('title').trim()==''||
	        r.get('date').trim()==''||
	        r.get('type').trim()==''||
	        r.get('rank').trim()==''||
	        r.get('place').trim()==''||
	        r.get('organizer').trim()==''||
	        r.get('masterOfCeremony').trim()==''||
	        r.get('chairman').trim()==''||
	        r.get('music').trim()==''||
	        r.get('scrutineer').trim()==''||
	        r.get('support').trim()=='') {
            /*this.markInvalid(
                editEvent.grid,
                editEvent.row,
                false,
                "Event title<br>must not be empty."
            );*/
            return;
        }
        if (editEvent.grid.getStore().query('title', r.get('title'), false, false).getCount()>1) {
            this.markInvalid(
                editEvent.grid,
                editEvent.row,
                false,
                "Event title must be unique."
            );
            return;
        }
        this.markInvalid(editEvent.grid, editEvent.row,true);

        Ext.Ajax.request({
            url:'eventUpdate.htm',
            maskEl:editEvent.grid.getGridEl(),
            params:{
                id:r.get('id'),
                title:r.get('title').trim(),
                date:r.get('date').trim(),
                type:r.get('type').trim(),
                rank:r.get('rank').trim(),
                place:r.get('place').trim(),
                organizer:r.get('organizer').trim(),
                masterOfCeremony:r.get('masterOfCeremony').trim(),
                chairman:r.get('chairman').trim(),
                music:r.get('music').trim(),
                scrutineer:r.get('scrutineer').trim(),
                support:r.get('support').trim()
            },
            method:'POST',
            success:function(rsp) {
                var o = Ext.util.JSON.decode(rsp.responseText);
                if (o.success) {
                    //this.set('id',o.updateId);
                    //this.commit();
                    DLSS.eventsStore.reload();
                }
                else {
                    Ext.Ajax.fireEvent('requestexception');
                }
            },
            scope:r
        });
    },
    markInvalid:function(grid, row, valid, message) {
        var r = grid.getView().getRow(row);
        if (valid) {
            Ext.get(r).removeClass('x-form-invalid');
            Ext.QuickTips.unregister(r.firstChild.firstChild.firstChild);
            var cr = r.firstChild.firstChild.firstChild;
            for (var i=0;x<cr.childNodes.length;i++) {
                Ext.QuickTips.unregister(cr.childNodes[i].firstChild);
            }
        }
        else {
            var nr = r.firstChild.firstChild.firstChild;
            for (var x=0;x<nr.childNodes.length;x++) {

                Ext.QuickTips.register({
                    target:nr.childNodes[x].firstChild,
                    text:message,
                    cls:'x-form-invalid-tip',
                    dismissDelay:5000
                });
            }
            Ext.get(r).removeClass('x-form-invalid');
            Ext.get(r).addClass('x-form-invalid');
        }
    }
    
});
    
DLSS.Main.sm = new Ext.grid.RowSelectionModel({singleSelect:'true'}); 
